package com.wynn.controller;


import com.wynn.pojo.Order;
import com.wynn.pojo.OrderException;
import com.wynn.pojo.Result;
import com.wynn.serviceFeign.OrderClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("orders")
public class OrderController {

    @Autowired
    private OrderClient orderClient;

    //统计本季度异常订单
    @GetMapping("excNum")
    public Result excNum(){

         return orderClient.statisticExc();
    }

    //统计本季度正在加工订单
    @GetMapping("num")
    public Result doingNum(){

        return orderClient.statisticOrder();
    }

    //统计本季度已完成订单
    @GetMapping("doneNum")
    public Result doneNum(){
        //统计为加工订单
        return orderClient.statisticDone();
    }

    //获取首页统计图二数据
    @GetMapping("ChartData2")
    public Result chartData2(){
        return orderClient.getChartData2();
    }

    //获取订单列表
    @GetMapping("selectAll")
    public Result selectAll(){
        Result result = new Result();
        try{
            List<Order> orders = orderClient.selectAll();
            result.setCode("200");
            result.setData(orders);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("503");
            result.setMessage("查询失败");
        }
        return result;
    }

    @GetMapping("search")
    public Result search(String deadLine, String company , Integer product){
        Result result = new Result();
        try {
            List<Order> orders = orderClient.searchAll(deadLine,company,product);
            result.setData(orders);
            result.setCode("200");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("503");
            result.setMessage("订单加载失败");
            log.warn("订单加载失败");
        }
        return result;
    }

    @PostMapping("deleteOrder")
    @PreAuthorize("hasRole('admin')")
    public Result deleteOrder(@RequestBody Order order){
        Result result = new Result();
        try{
            orderClient.deleteOrder(order.getId());
            result.setMessage("订单删除成功");
            result.setCode("200");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("203");
            result.setMessage("订单删除失败");
        }

        return result;
    }

    @PostMapping("addOrder")
    @PreAuthorize("hasRole('admin')")
    public Result addOrder(@RequestBody Order order){
        Result result = new Result();
        try{
            orderClient.addOrder(order);
            result.setCode("200");
            result.setMessage("添加订单成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("添加订单失败");
            result.setCode("503");
        }
        return result;
    }

    @PostMapping("editOrder")
    @PreAuthorize("hasRole('admin')")
    public Result editOrder(@RequestBody Order order){
        Result result = new Result();
        try{
            orderClient.editOrder(order);

            result.setCode("200");
            result.setMessage("修改订单成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("503");
            result.setMessage("修改订单失败");
            log.info("修改订单失败");
        }
        return result;
    }

    @PostMapping("addException")
    @PreAuthorize("hasRole('admin')")
    public Result addException(@RequestBody Map<String,String> orderException) {
        Result result = new Result();
        try{
            boolean flag = orderClient.addOrderException(orderException);
            if(flag==true){
                result.setCode("200");
                result.setMessage("上报异常成功");
            }else {
                result.setCode("500");
                result.setMessage("该订单已经设为异常");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("503");
            result.setMessage("上报异常失败");
            log.warn("上报异常失败");
        }
        return result;
    }

    @PostMapping("submit")
    @PreAuthorize("hasRole('admin')")
    public Result submit(@RequestBody Order order)  {
        Result result = new Result();
        try{
            orderClient.submit(order);
            result.setCode("200");
            result.setMessage("交货成功");

        }catch (Exception e){
            e.printStackTrace();
            result.setCode("503");
            result.setMessage("交货失败");
            log.warn("交货失败");
        }
        return result;
    }

    @PostMapping("searchException")
    public Result searchException(@RequestBody Map<String,String> map){
        Result result = new Result();
        try{
            List<OrderException> orderExceptions = orderClient.searchException(map);

            result.setData(orderExceptions);
            result.setCode("200");
            result.setMessage("加载异常订单成功");

        }catch (Exception e){
            e.printStackTrace();
            result.setCode("503");
            result.setMessage("加载异常订单失败");
            log.warn("加载异常订单失败");
        }
        return result;
    }

    @PostMapping("editException")
    @PreAuthorize("hasRole('admin')")
    public Result editException(@RequestBody OrderException orderException){
        Result result = new Result();
        try {
            orderClient.editException(orderException);
            result.setCode("200");
            result.setMessage("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("503");
            result.setMessage("修改失败");
        }
        return result;

    }

    @PostMapping("recoverException")
    @PreAuthorize("hasRole('admin')")
    public Result recoverException(int id,int orderId){
        Result result = new Result();
        try {
            orderClient.recoverException(id,orderId);
            result.setCode("200");
            result.setMessage("订单恢复成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode("503");
            result.setMessage("订单恢复失败");
        }
        return result;

    }
}
